/*
Prepared by Nguyen Minh Huy
March 2012
*/
#pragma once

#include "stdint.h"

#include <stdio.h>

struct BmpCode
{
	unsigned char data[2];	// "BM"
};

struct BmpHeader
{
	uint32_t fileSize;		// size
	uint16_t reserved1;		// 0
	uint16_t reserved2;		// 0
	uint32_t dataOffset;	// 1078
};

struct BmpDib
{
	uint32_t dibSize;			// 40
	int32_t	 imageWidth;		// 100
	int32_t  imageHeight;		// 100
	uint16_t colorPlaneCount;	// 1
	uint16_t pixelSize;			// 1: B/W; 4: 16 colors; 8: 256 colors; 24: 16.7m 		
	uint32_t compressMethod;	// 0
	uint32_t bitmapByteCount;	// 0
	int32_t  horizontalResolution;	// 0
	int32_t  verticalResolution;	// 0
	uint32_t colorCount;			// 0
	uint32_t importantColorCount;	
};

struct Pixel24
{
	unsigned char blue;
	unsigned char green;
	unsigned char red;
};

struct Pixel24Line
{
	Pixel24  *pixels;
	uint32_t length;
};

struct Pixel24Array
{
	Pixel24Line *lines;
	uint32_t    length;
};

bool isBmpFile(FILE *f);

void readBmpHeader(FILE *f, BmpHeader &header);
void printBmpHeader(BmpHeader header);

void readBmpDib(FILE *f, BmpDib &dib);
void printBmpDib(BmpDib dib);

void readBmpPixelArray(FILE *f, BmpHeader header, BmpDib dib, Pixel24Array &data);
void readBmpPixelLine(FILE *f, BmpDib dib, Pixel24Line &line);
void skipBmpPadding(FILE *f, BmpDib dib);

void drawBmp(BmpDib dib, Pixel24Array data);

void drawBmp(uint32_t imageWidth, uint32_t imageHeight, Pixel24Array data);

void releaseBmpPixelArray(Pixel24Array data);